package com.qyhl.webtv.module_user.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.orhanobut.hawk.Hawk;
import com.qinanyu.bannerview.holder.SimpleHolder;
import com.qinanyu.bannerview.view.SimpleBannerView;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.SpanUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.dialog.LoadingDialog;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.constant.AppConfigConstant;
import com.qyhl.webtv.commonlib.entity.VersionBean;
import com.qyhl.webtv.commonlib.entity.config.AppConfigBean;
import com.qyhl.webtv.commonlib.entity.intergral.ExchangerRateBean;
import com.qyhl.webtv.commonlib.entity.intergral.IntegralUserInfoBean;
import com.qyhl.webtv.commonlib.entity.user.LuckDrawAdvBean;
import com.qyhl.webtv.commonlib.entity.user.MessageUnreadBean;
import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.qyhl.webtv.commonlib.service.UserService;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.cache.SpfManager;
import com.qyhl.webtv.commonlib.utils.config.AppConfigUtil;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.commonlib.utils.inter.HomeActivityInterface;
import com.qyhl.webtv.commonlib.utils.push.PushTools;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.commonlib.utils.view.CircleImageView;
import com.qyhl.webtv.module_user.R;
import com.qyhl.webtv.module_user.R2;
import com.qyhl.webtv.module_user.common.UserUrl;
import com.qyhl.webtv.module_user.util.LoginUtils;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;
import q.rorbin.badgeview.QBadgeView;

/**
 * Created by qyhl on 2017/11/7.
 * 个人中心
 */

public class UserCenterFragment extends BaseFragment implements UserCenterContract.UserCenterView {
    @BindView(R2.id.head_icon)
    CircleImageView headIcon;
    @BindView(R2.id.user_nickname)
    TextView userNickname;
    @BindView(R2.id.gold_exchangerate)
    TextView gold_exchangerate;
    @BindView(R2.id.gold_coin)
    TextView gold_coin;
    @BindView(R2.id.btn_signin)
    Button btn_signin;
    @BindView(R2.id.user_my_scoop)
    RelativeLayout userMyScoop;
    @BindView(R2.id.user_scoop_follow)
    RelativeLayout userScoopFollow;
    @BindView(R2.id.user_my_collection)
    RelativeLayout userMyCollection;
    @BindView(R2.id.user_my_activity)
    RelativeLayout userMyActivity;
    @BindView(R2.id.user_setting)
    RelativeLayout userSetting;
    @BindView(R2.id.recommend)
    RelativeLayout recommend;
    @BindView(R2.id.signin_img)
    ImageView signin_img;
    @BindView(R2.id.invite_code)
    EditText inviteCode;
    @BindView(R2.id.commit_code_button)
    TextView commitCodeButton;
    @BindView(R2.id.scrollview)
    ScrollView scrollview;
    @BindView(R2.id.user_my_scoop_txt)
    TextView userMyScoopTxt;
    @BindView(R2.id.user_my_follow_txt)
    TextView userMyFollowTxt;
    @BindView(R2.id.user_my_home)
    RelativeLayout userMyHome;
    @BindView(R2.id.friend_fans)
    RelativeLayout friend_fans;
    @BindView(R2.id.checkoutNew)
    TextView checkoutNew;
    @BindView(R2.id.message)
    TextView message;
    @BindView(R2.id.sign_in_text)
    TextView signin_txt;
    @BindView(R2.id.user_invite_code)
    LinearLayout user_invite_code;
    @BindView(R2.id.fengexian)
    View fengexian;
    @BindView(R2.id.user_check)
    TextView userCheck;


    @BindView(R2.id.adv_banner)
    SimpleBannerView mAdvBanner;

    @BindView(R2.id.adv_layout)
    LinearLayout advLayout;
    @BindView(R2.id.taskcenter)
    RelativeLayout taskcenter;

    @BindView(R2.id.divider_taskcenter)
    View dividerTaskcenter;

    @BindView(R2.id.commonproblem)
    RelativeLayout commonproblem;

    @BindView(R2.id.user_top_layout)
    RelativeLayout user_top_layout;

    @BindView(R2.id.user_my_exchange)
    RelativeLayout user_my_exchange;

    @BindView(R2.id.divider_comprogram)
    View dividerComprogram;

    @BindView(R2.id.exchange_layout)
    LinearLayout exchangeLayout;

    @BindView(R2.id.score_layout)
    CardView scoreLayout;


    private LoadingDialog.Builder builder;

    private boolean hasScoop = false;//是否有内容

    private boolean hasCircle = false;//是否有圈子

    private RequestOptions options;

    private UserCenterPresenter mPresenter;

    private static final float MIN_ALPHA = 0.0f;

    /**
     * 数据是否第一次加载
     */
    private boolean mIsInited;
    /**
     * 布局界面初始化
     */
    private boolean mIsPrepared;
    private boolean commitCodeEnable = true;
    //当前金币数量
    private int nowCoin = 0;
    //用户积分信息
    private IntegralUserInfoBean infoBean;

    private List<LuckDrawAdvBean> mAdvList = new ArrayList<>();

    private QBadgeView badgeView;

    HomeActivityInterface homeActivityInterface;

    public void setHomeActivityInterface(HomeActivityInterface homeActivityInterface) {
        this.homeActivityInterface = homeActivityInterface;
    }

    public static UserCenterFragment newInstance(HomeActivityInterface homeActivityInterface) {
        UserCenterFragment userCenterFragment = new UserCenterFragment();
        userCenterFragment.setHomeActivityInterface(homeActivityInterface);
        return userCenterFragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.login_fragment_user_center, container, false);
    }

    @Override
    protected void create() {
        getMessage();
        initView();
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(user_top_layout)
                .init();
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {
        getMessage();
        if (Hawk.contains("login")) {
            //请求金币数量和签到信息
            mPresenter.getIntegralData();
            //请求汇率
            mPresenter.getExchangerRate();
        }
        String inviteCode = CommonUtils.getInstance().getInviteCode();
        if (StringUtils.isNotEmpty(inviteCode)) {
            user_invite_code.setVisibility(View.GONE);
            fengexian.setVisibility(View.GONE);
        } else {
            user_invite_code.setVisibility(View.VISIBLE);
            fengexian.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onVisible() {
        getMessage();
        if (Hawk.contains("login")) {
            //请求金币数量和签到信息
            mPresenter.getIntegralData();
            //请求汇率
            mPresenter.getExchangerRate();

            if (CommonUtils.getInstance().getSite() == 217) {
                mPresenter.getUserInfo(CommonUtils.getInstance().getUserName());
            }
        }
        String inviteCode = CommonUtils.getInstance().getInviteCode();
        if (StringUtils.isNotEmpty(inviteCode)) {
            user_invite_code.setVisibility(View.GONE);
            fengexian.setVisibility(View.GONE);
        } else {
            user_invite_code.setVisibility(View.VISIBLE);
            fengexian.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onHide() {

    }


    /**
     * 初始化
     */
    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        mPresenter = new UserCenterPresenter(this);
        BusFactory.getBus().register(this);
        //点击水波纹效果
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            userMyScoop.setBackgroundResource(R.drawable.center_item_ripple);
            userScoopFollow.setBackgroundResource(R.drawable.center_item_ripple);
            userMyCollection.setBackgroundResource(R.drawable.center_item_ripple);
            userMyActivity.setBackgroundResource(R.drawable.center_item_ripple);
            userSetting.setBackgroundResource(R.drawable.center_item_ripple);
            userMyHome.setBackgroundResource(R.drawable.center_item_ripple);
            friend_fans.setBackgroundResource(R.drawable.center_item_ripple);
            recommend.setBackgroundResource(R.drawable.center_item_ripple);
            user_my_exchange.setBackgroundResource(R.drawable.center_item_ripple);
        }

        /*
         *青城眼
         */
        if (CommonUtils.getInstance().getSite() == 174) {
            userMyScoopTxt.setText("我的");
            userMyFollowTxt.setText("关注");
        }

        String integralSwitch = CommonUtils.getInstance().getIntergralSwitch();
        if (integralSwitch.equals("1")) {
            taskcenter.setVisibility(View.VISIBLE);
            dividerTaskcenter.setVisibility(View.VISIBLE);
            signin_img.setVisibility(View.VISIBLE);
            signin_txt.setVisibility(View.VISIBLE);
            commonproblem.setVisibility(View.VISIBLE);
            dividerComprogram.setVisibility(View.VISIBLE);
            exchangeLayout.setVisibility(View.VISIBLE);
            scoreLayout.setVisibility(View.VISIBLE);
            user_top_layout.setBackgroundResource(R.drawable.user_head);
            btn_signin.setTextColor(getResources().getColor(R.color.white));
            btn_signin.setBackgroundResource(R.drawable.login_btn_signin_bg);
            userNickname.setTextColor(getResources().getColor(R.color.white));
        } else {
            taskcenter.setVisibility(View.GONE);
            dividerTaskcenter.setVisibility(View.GONE);
            signin_img.setVisibility(View.GONE);
            signin_txt.setVisibility(View.GONE);
            commonproblem.setVisibility(View.GONE);
            dividerComprogram.setVisibility(View.GONE);
            exchangeLayout.setVisibility(View.GONE);
            scoreLayout.setVisibility(View.GONE);
            user_top_layout.setBackgroundResource(R.drawable.user_head_old);
            btn_signin.setTextColor(getResources().getColor(R.color.global_black_lv2));
            btn_signin.setBackgroundResource(R.drawable.login_btn_signin_bg_two);
            userNickname.setTextColor(getResources().getColor(R.color.global_black_lv1));
        }
        //没有爆料不显示爆料关注和我的爆料
        AppConfigBean bean = Hawk.get("config");
        List<AppConfigBean.tabMenus> tabMenusLists = bean.getTabMenus();
        List<AppConfigBean.SecMenus> seconedMenuLists = bean.getSeconedMenus();
        for (int i = 0; i < tabMenusLists.size(); i++) {
            if (tabMenusLists.get(i).getTypeId() == 13) {
                hasScoop = true;
            }
            if (tabMenusLists.get(i).getTypeId() == 17) {
                hasCircle = true;
            }
        }
        for (int i = 0; i < seconedMenuLists.size(); i++) {
            if (seconedMenuLists.get(i).getTypeId() == 19 || seconedMenuLists.get(i).getTypeId() == 23) {
                hasScoop = true;
            }
        }
        if (hasScoop) {
            userMyScoop.setVisibility(View.VISIBLE);
            userScoopFollow.setVisibility(View.VISIBLE);
        } else {
            userMyScoop.setVisibility(View.GONE);
            userScoopFollow.setVisibility(View.GONE);
        }
        /*
         * 是否有圈子
         */
        if (hasCircle) {
            userMyHome.setVisibility(View.VISIBLE);
            friend_fans.setVisibility(View.VISIBLE);
        } else {
            userMyHome.setVisibility(View.GONE);
            friend_fans.setVisibility(View.GONE);
        }

        builder = new LoadingDialog.Builder(getActivity());
        builder.setTitle("请稍等...");
        builder.setCanceledOnTouchOutside(false);
        builder.setCancelable(true);

        options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.comment_head_default)
                .error(R.drawable.comment_head_default)
                .priority(Priority.HIGH);

        inviteCode.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                inviteCode.requestFocus();
            }
            return false;
        });

        inviteCode.setOnClickListener(v -> homeActivityInterface.setTabbarVisibility());
        inviteCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtils.isEmpty(s.toString())) {
                    commitCodeButton.setEnabled(false);
                    commitCodeButton.setTextColor(getActivity().getResources().getColor(R.color.global_gray_lv2));
                } else {
                    commitCodeButton.setEnabled(true);
                    commitCodeButton.setTextColor(getActivity().getResources().getColor(R.color.global_base));
                }
            }
        });

        setGoldCoin(nowCoin);
        if (Hawk.contains("login")) {
            userNickname.setVisibility(View.VISIBLE);
            btn_signin.setVisibility(View.GONE);
            //请求金币数量和签到信息
            mPresenter.getIntegralData();
            //请求汇率
            mPresenter.getExchangerRate();
            String headicon = CommonUtils.getInstance().getUserLogo();
            String nickName = CommonUtils.getInstance().getNickName();
            userNickname.setText(nickName);
            Glide.with(this)
                    .load(headicon)
                    .apply(options)
                    .into(headIcon);

            switch (CommonUtils.getInstance().getUserCheck()) {
                case "0":
                    userCheck.setVisibility(View.VISIBLE);
                    userCheck.setText("(审核中)");
                    break;
                case "2":
                    userCheck.setVisibility(View.VISIBLE);
                    userCheck.setText("(" + CommonUtils.getInstance().getUserCheckReason() + ")");
                    break;
                case "1":
                default:
                    userCheck.setVisibility(View.GONE);
                    break;
            }

            if (CommonUtils.getInstance().getIntergralSwitch().equals("1")) {
                gold_exchangerate.setVisibility(View.VISIBLE);
                gold_coin.setVisibility(View.VISIBLE);
            }
        }
        //获取当前版本号
        try {
            PackageManager manager = getActivity().getPackageManager();
            PackageInfo info = manager.getPackageInfo(getActivity().getPackageName(), 0);
            VersionBean.DataBean versionBean = CommonUtils.getInstance().getVersionData();
            if (versionBean != null && (info.versionCode < versionBean.getCode())) {
                checkoutNew.setVisibility(View.VISIBLE);
            }
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mPresenter.getAdvData();
    }

    //设置金币数量
    private void setGoldCoin(int num) {
        SpanUtils spanUtils = new SpanUtils(getContext());
        spanUtils.append(String.valueOf(num)).append("金币").setSpans(new TextAppearanceSpan(getActivity(), R.style.login_goldcointext));
        gold_coin.setText(spanUtils.create());
    }


    /**
     * 监听
     *
     * @param view
     */
    @OnClick({R2.id.head_icon, R2.id.commonproblem, R2.id.friend_fans, R2.id.message, R2.id.putforward, R2.id.waller, R2.id.taskcenter, R2.id.btn_signin,
            R2.id.recommend, R2.id.user_my_scoop, R2.id.user_scoop_follow, R2.id.user_my_collection, R2.id.user_my_activity, R2.id.user_setting,
            R2.id.commit_code_button, R2.id.user_my_exchange, R2.id.user_my_home, R2.id.signin_img})
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.commonproblem) {//常见问题
            RouterManager.router(ARouterPathConstant.COMMON_PROBLEM);
        } else if (i == R.id.waller) {
            RouterManager.routerLoginInterceptor(getContext(), ARouterPathConstant.WALLER);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.USER_WALLER_HOME);

        } else if (i == R.id.message) {
            RouterManager.routerLoginInterceptor(getContext(), ARouterPathConstant.NEW_MESSAGE);
        } else if (i == R.id.putforward) {
            LoginUtils.checkLogin(new LoginUtils.LoginCallBack() {
                @Override
                public void login(boolean isLogin) {
                    if (isLogin) {
                        if (infoBean != null) {
                            Bundle bundle = new Bundle();
                            bundle.putString("alipayName", infoBean.getAlipayName());
                            bundle.putString("alipayAccount", infoBean.getAlipayAccount());
                            bundle.putDouble("currMoney", infoBean.getCurrMoney());
                            bundle.putString("wxName", infoBean.getWxName());
                            bundle.putString("wxOpenid", infoBean.getWxOpenid());
                            RouterManager.router(ARouterPathConstant.PUTFORWARD, bundle);
                        }
                    } else {
                        RouterManager.routerLogin(getContext(), 0);
                        Toasty.normal(getContext(), "尚未登录或登录已失效！", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void error(String s) {

                }
            });

        } else if (i == R.id.signin_img || i == R.id.sign_in_text) {
            RouterManager.router(ARouterPathConstant.TASK_CENTER);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.SIGN_IN_HOME);

        } else if (i == R.id.head_icon) {
            RouterManager.routerLoginInterceptor(getContext(), ARouterPathConstant.USERINFO);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.USERINFO);

        } else if (i == R.id.taskcenter) {
            RouterManager.router(ARouterPathConstant.TASK_CENTER);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.SIGN_IN_HOME);
        } else if (i == R.id.btn_signin) {
            RouterManager.routerLogin(getContext(), 0);

        } else if (i == R.id.user_my_scoop) {
            RouterManager.routerLoginInterceptor(getContext(), ARouterPathConstant.SCOOP_MINE);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.SCOOPMINE);

        } else if (i == R.id.user_scoop_follow) {
            RouterManager.routerLoginInterceptor(getContext(), ARouterPathConstant.SCOOP_ATTENTION);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.SCOOPATTENTION);

        } else if (i == R.id.user_my_collection) {
            RouterManager.routerLoginInterceptor(getContext(), ARouterPathConstant.COLLECT);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.COLLECT);

        } else if (i == R.id.user_my_activity) {
            RouterManager.routerLoginInterceptor(getContext(), ARouterPathConstant.ACTIVITY_MINE);

        } else if (i == R.id.user_setting) {
            RouterManager.router(ARouterPathConstant.SETTING);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.SETTING);

        } else if (i == R.id.commit_code_button) {
            if (commitCodeEnable) {
                commitCodeEnable = false;
                LoginUtils.checkLogin(new LoginUtils.LoginCallBack() {
                    @Override
                    public void login(boolean isLogin) {
                        if (isLogin) {
                            String code = inviteCode.getText().toString();
                            if (StringUtils.isNotEmpty(code)) {
                                builder.show();
                                mPresenter.sendCode(code);
                                ActionLogUtils.getInstance().onEvent(ActionConstant.COMMITE_CODE);
                            } else {
                                Toasty.normal(getContext(), "邀请码不能为空！", Toast.LENGTH_SHORT).show();
                                commitCodeEnable = true;
                            }
                        } else {
                            RouterManager.routerLogin(getContext(), 0);
                            Toasty.normal(getContext(), "尚未登录或登录已失效！", Toast.LENGTH_SHORT).show();
                            commitCodeEnable = true;
                        }
                    }

                    @Override
                    public void error(String s) {
                        commitCodeEnable = true;
                    }
                });
            }
        } else if (i == R.id.user_my_home) {
            Bundle bundle1 = new Bundle();
            String userName2 = CommonUtils.getInstance().getUserName();
            bundle1.putString("dstusername", userName2);
            RouterManager.routerLoginInterceptor(getContext(), bundle1, ARouterPathConstant.CIRCLE_USER);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.CIRCLEUSER);

        } else if (i == R.id.recommend) {
            RouterManager.routerLoginInterceptor(getContext(), ARouterPathConstant.SHARE_HOME);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.SHAREQRCODE);

        } else if (i == R.id.friend_fans) {
            CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                @Override
                public void login(boolean isLogin) {
                    if (isLogin) {
                        Bundle bundle2 = new Bundle();
                        bundle2.putInt("type", 0);
                        RouterManager.router(ARouterPathConstant.USER_LIST, bundle2);
                        ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.FRIENDFANS);
                    } else {
                        RouterManager.routerLogin(getActivity(), 0);
                    }
                }

                @Override
                public void error(String s) {

                }
            });

        } else if (i == R.id.user_my_exchange) {
            CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                @Override
                public void login(boolean isLogin) {
                    if (isLogin) {
                        RouterManager.router(ARouterPathConstant.SHOP_EXCHANGE);
                    } else {
                        RouterManager.routerLogin(getActivity(), 0);
                    }
                }

                @Override
                public void error(String s) {

                }
            });
        }
    }

    /**
     * eventBus 接收器
     *
     * @param event
     */
    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(Event.LoginMessage event) {
        if (event != null) {
            infoBean = null;
            if (event.isLogin()) {//登录
                userNickname.setVisibility(View.VISIBLE);
                if (CommonUtils.getInstance().getIntergralSwitch().equals("1")) {
                    gold_exchangerate.setVisibility(View.VISIBLE);
                    gold_coin.setVisibility(View.VISIBLE);
                }
                btn_signin.setVisibility(View.GONE);
                //请求金币数量和签到信息
                mPresenter.getIntegralData();
                //请求汇率
                mPresenter.getExchangerRate();
                String headicon = CommonUtils.getInstance().getUserLogo();
                String nickName = CommonUtils.getInstance().getNickName();
                userNickname.setText(nickName);
                Glide.with(this)
                        .load(headicon)
                        .apply(options)
                        .into(headIcon);

                switch (CommonUtils.getInstance().getUserCheck()) {
                    case "0":
                        userCheck.setVisibility(View.VISIBLE);
                        userCheck.setText("(审核中)");
                        break;
                    case "2":
                        userCheck.setVisibility(View.VISIBLE);
                        userCheck.setText("(" + CommonUtils.getInstance().getUserCheckReason() + ")");
                        break;
                    case "1":
                    default:
                        userCheck.setVisibility(View.GONE);
                        break;
                }

            } else {//注销
                signin_img.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.user_signin_button_normal));
                if (Hawk.contains("token")) {
                    Hawk.delete("token");
                }

                if (Hawk.contains("login")) {
                    Hawk.delete("login");
                }


                CommonUtils.getInstance().putConfigData(AppConfigConstant.NICK_NAME, "");
                CommonUtils.getInstance().putConfigData(AppConfigConstant.USER_NAME, "");
                CommonUtils.getInstance().putConfigData(AppConfigConstant.LOGIN_TOKEN, "");
                userNickname.setVisibility(View.GONE);
                gold_exchangerate.setVisibility(View.GONE);
                gold_coin.setVisibility(View.GONE);
                btn_signin.setVisibility(View.VISIBLE);
                userCheck.setVisibility(View.GONE);

                Glide.with(this)
                        .load(R.drawable.comment_head_default)
                        .apply(options)
                        .into(headIcon);
                //解除绑定账号
                PushTools.unBindAccount();

            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshFollowNum(Event.followNumRefresh event) {

    }

    /**
     * 邀请码提交成功
     *
     * @param msg
     */
    @Override
    public void sendSuccess(String msg) {
        user_invite_code.setVisibility(View.GONE);
        fengexian.setVisibility(View.GONE);
        //随便赋个值控制个人中心的显示
        UserInfoBean login = Hawk.get("login");
        login.setInviteCode(inviteCode.getText().toString());
        CommonUtils.getInstance().putConfigData(AppConfigConstant.INVITE_CODE, inviteCode.getText().toString());
        Hawk.put("login", login);

        builder.dismiss();
        showToast(msg);
        inviteCode.setText("");
        commitCodeEnable = true;

        user_invite_code.setVisibility(View.GONE);
        fengexian.setVisibility(View.GONE);
    }

    /**
     * 邀请码提交失败
     *
     * @param msg
     */
    @Override
    public void sendError(String msg) {
        builder.dismiss();
        showToast(msg);
        inviteCode.setText("");
        commitCodeEnable = true;
    }

    /**
     * 积分数据请求失败
     */
    @Override
    public void integralError(String msg) {
        msg = TextUtils.isEmpty(msg) ? "获取失败" : msg;
        showToast(msg);
    }

    /**
     * 积分数据请求成功
     */
    @SuppressLint("SetTextI18n")
    @Override
    public void integralSuccess(IntegralUserInfoBean data) {
        infoBean = data;
        nowCoin = data.getCurrCoin() == null ? 0 : data.getCurrCoin();
        setGoldCoin(nowCoin);
        gold_exchangerate.setText("今日金币：" + (data.getTodayCoin() == null ? 0 : data.getTodayCoin()));
        if (data.getSign() == null ? true : data.getSign()) {
            //如果签到了
            signin_img.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.user_signin_button_visited));
        }
    }

    /**
     * 汇率请求失败
     */
    @Override
    public void exchangerRateError() {

    }

    /**
     * 汇率请求成功
     */
    @Override
    public void exchangerRateSuccess(ExchangerRateBean data) {
//        gold_exchangerate.setText("当前参考汇率: " + ((float) 1 / (float) data.getRate()));
    }

    @Override
    public void setAdvList(List<LuckDrawAdvBean> beanList) {
        mAdvList.clear();
        if (beanList != null) {
            mAdvList.addAll(beanList);
            advLayout.setVisibility(View.VISIBLE);
            if (beanList.size() == 0) {
                advLayout.setVisibility(View.GONE);
            } else if (beanList.size() == 1) {  //一张图时关闭轮播
                advLayout.setVisibility(View.VISIBLE);
                mAdvBanner.setCanLoop(false);
            } else {
                advLayout.setVisibility(View.VISIBLE);
                mAdvBanner.setCanLoop(true);
            }

            mAdvBanner.setPages(() -> new AdvsImageHolderView(), beanList)
                    .startTurning(4000)
                    .setPointViewVisible(false)
                    .canTouchScroll(false)
                    .setPageTransformer((page, position) -> {
                        int pageWidth = page.getWidth();    //得到view宽

                        if (position < -1) { // [-Infinity,-1)
                            // This page is way off-screen to the left. 出了左边屏幕
                            page.setAlpha(0);

                        } else if (position <= 1) { // [-1,1]
                            if (position < 0) {
                                //消失的页面
                                page.setTranslationX(-pageWidth * position);  //阻止消失页面的滑动
                            } else {
                                //出现的页面
                                page.setTranslationX(pageWidth);        //直接设置出现的页面到底
                                page.setTranslationX(-pageWidth * position);  //阻止出现页面的滑动
                            }
                            // Fade the page relative to its size.
                            float alphaFactor = Math.max(MIN_ALPHA, 1 - Math.abs(position));
                            //透明度改变Log
                            page.setAlpha(alphaFactor);
                        } else { // (1,+Infinity]
                            // This page is way off-screen to the right.    出了右边屏幕
                            page.setAlpha(0);
                        }
                    })
                    .hideBannerText(true);

        } else {
            advLayout.setVisibility(View.GONE);
        }

        mAdvBanner.setOnItemClickListener(position -> {
            Bundle bundle = new Bundle();
            String userName = CommonUtils.getInstance().getUserName();
            bundle.putString("userName", userName);
            bundle.putInt("id", mAdvList.get(position).getActId());
            RouterManager.router(ARouterPathConstant.NINE_LUCK_DRAW, bundle);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.USERCENTER, ActionConstant.LOTTERY);
        });
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void userInfoSuccess(UserInfoBean bean) {
        if (!CommonUtils.getInstance().getUserCheck().equals(bean.getChecked())) {
            AppConfigUtil config = AppConfigUtil.getInstance();
            config.setConValue(AppConfigConstant.USER_CHECK, bean.getChecked());
            config.setConValue(AppConfigConstant.USER_CHECK_REASON, bean.getFailReason());

            switch (bean.getChecked()) {
                case "0":
                    userCheck.setVisibility(View.VISIBLE);
                    userCheck.setText("(审核中)");
                    break;
                case "2":
                    userCheck.setVisibility(View.VISIBLE);
                    userCheck.setText("(" + CommonUtils.getInstance().getUserCheckReason() + ")");
                    break;
                case "1":
                default:
                    userCheck.setVisibility(View.GONE);
                    break;
            }
        }
    }

    @Override
    public void userInfoError(String msg) {

    }


    @Override
    public void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("个人中心");
        MobclickAgent.onResume(getContext());
    }

    @Override
    public void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("个人中心");
        MobclickAgent.onPause(getContext());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BusFactory.getBus().unregister(this);
    }


    /**
     * 广告轮播holder
     */
    class AdvsImageHolderView implements SimpleHolder<LuckDrawAdvBean> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, LuckDrawAdvBean data) {
            Glide.with(context.getApplicationContext())
                    .load(data.getLogo())
                    .apply(new RequestOptions().placeholder(R.drawable.adv_group_no_img).error(R.drawable.adv_group_no_img))
                    .into(imageView);
        }
    }


    private void getMessage() {
        if (hasCircle) {

            EasyHttp.get(UserUrl.GET_UNREAD_MSG_NUM)
                    .params("siteId", CommonUtils.getInstance().getSite() + "")
                    .params("devType", "Android")
                    .params("sTime", SpfManager.getInstance(getContext()).getString("last_system_time", "0"))
                    .params("tTime", SpfManager.getInstance(getContext()).getString("last_tip_time", "0"))
                    .params("userName", CommonUtils.getInstance().getUserName())
                    .execute(new SimpleCallBack<MessageUnreadBean>() {
                        @Override
                        public void onError(ApiException e) {

                        }

                        @Override
                        public void onSuccess(MessageUnreadBean messageUnreadBean) {
                            CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                                @Override
                                public void login(boolean isLogin) {
                                    if (isLogin) {
                                        if (messageUnreadBean.getuInteractive() > 0 || messageUnreadBean.getuTips() > 0 || messageUnreadBean.getuSystem() > 0) {
                                            if (badgeView == null) {
                                                badgeView = new QBadgeView(getContext());
                                            }
                                            badgeView.bindTarget(message).setGravityOffset(25, -3, true).setBadgeNumber(messageUnreadBean.getuSystem() + messageUnreadBean.getuTips() + messageUnreadBean.getuInteractive());
                                        } else {
                                            if (badgeView != null) {
                                                badgeView.hide(false);
                                            }
                                        }
                                    } else {
                                        if (badgeView != null) {
                                            badgeView.hide(false);
                                        }
                                    }
                                }

                                @Override
                                public void error(String s) {

                                }
                            });
                        }
                    });


        }
    }

}
